/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: com_message.h
 *
 * Purpose: communication message structure define and implementation
 *
 * Developer:
 *   wen.gu , 2023-06-28
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __UDSSIPC_COM_MESSAGE_H__
#define __UDSSIPC_COM_MESSAGE_H__

#include <cstdint>
#include <memory>
#include "core/udssipc_type.h"

#define UDSSIPC_COM_MSG_START_CODE 0xabcd
#define UDSSIPC_COM_PROTOCOL_VER 0x1
#define UDSSIPC_COM_INVALID_SESSION_ID 0



enum class ComMessageType: uint8_t {
    Unknown = 0,
    SyncEvent,
};

struct ComMessageHeader {
    uint32_t length;  /** total message size = header size + payloaf size */
    uint16_t start_code;
    uint8_t protocol_version;
    ComMessageType type;
};

class ComMessage {
public:
    using ComMessagePtr = std::shared_ptr<ComMessage>;
protected:
    ComMessage(ComMessageType type, uint32_t payload_size, uint8_t* payload = nullptr);
    ComMessage(const ComMessageHeader& hdr);
public:
    ~ComMessage();

public:
    uint8_t* data() const { return (uint8_t*)msg_header_; }
    uint32_t length() const { return msg_header_->length; }
    uint16_t start_core() const { return msg_header_->start_code; }
    uint8_t protocol_version() const { return msg_header_->protocol_version; }
    ComMessageType type() const { return msg_header_->type; }

    uint8_t* payload() { return ((uint8_t*)msg_header_) + sizeof(*msg_header_); }
    uint32_t payload_size() { return msg_header_->length - sizeof(*msg_header_); }

public: /** extension API, just as helper API, not as the data which to be sent */
    ComFd com_fd() const { return com_fd_; }
    void set_com_fd(ComFd fd) { com_fd_ = fd; } //this will be set by 'server' API
public:
    static ComMessagePtr Create(ComMessageType type, uint32_t payload_size, uint8_t* payload = nullptr);

    static ComMessagePtr Create(const ComMessageHeader& hdr);
private:
    ComMessageHeader* msg_header_ = nullptr;
    ComFd com_fd_ = INVALID_COM_FD;
};


using ComMessagePtr = ComMessage::ComMessagePtr;




#endif /** !__UDSSIPC_COM_MESSAGE_H__ */